home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.04 Apr 94 / Powering Up / Executing Code / Chaos.c < prev    next >
Encoding:
Text File  |  1994-02-09  |  1.8 KB  |  78 lines  |  [TEXT/MPCC]

  1. // File: Chaos.c
  2. //
  3. // This is a piece of code which can be loaded and called by SimpleApp
  4. // It draws a simple (but interesting) fractal
  5.  
  6. #include <Quickdraw.h>
  7. #include <Windows.h>
  8. #include <Events.h>
  9.  
  10. // === Function prototypes
  11. void DrawFractal (WindowPtr theWindow);
  12.  
  13.  
  14. // Our main routine can have any calling sequence we want
  15. // This particular version is defined in SimpleApp.c
  16. Boolean OurMainRoutine (EventRecord *theEvent, WindowPtr currWindow, QDGlobals *qdAddress)
  17. {
  18.     Boolean    handled = false;
  19.     
  20.     if (theEvent->what == updateEvt) {
  21.         WindowPtr    theWindow = (WindowPtr)theEvent->message;
  22.         
  23.         BeginUpdate(theWindow);
  24.         SetPort(theWindow);
  25.  
  26.         DrawFractal(theWindow);
  27.         
  28.         EndUpdate(theWindow);
  29.         handled = true;
  30.     }
  31.     
  32.     return handled;    // Did we handle the event?
  33. }
  34.  
  35.  
  36. void DrawFractal (WindowPtr theWindow)
  37. {
  38.     // This routine draws trhe fractal characterized by the population
  39.     // equation x = ax(1-x), for 2.95 ≤ a < 4.00
  40.     // At each value of a, we plot a few hundred values of x
  41.     
  42.     double    aMin = 2.95;
  43.     double    aMax = 4.00;
  44.     double    a, x;
  45.     double    deltaA;
  46.     Rect    portRect = theWindow->portRect;
  47.     Rect    imageRect = (*theWindow->visRgn)->rgnBBox;
  48.     double    portWidth = portRect.right - portRect.left;
  49.     short    v, h;
  50.     
  51.     // Set up the screen for drawing
  52.     EraseRect(&portRect);
  53.     PenNormal();
  54.     
  55.     deltaA = (aMax - aMin) / (portRect.bottom - portRect.top);
  56.     v = 0;
  57.     for (a = aMin; a < aMax; a += deltaA, v++) {
  58.         register int count;
  59.         
  60.         // Don't bother drawing if this isn't in the visible part of the window
  61.         if ((v < imageRect.top) | (v > imageRect.bottom))
  62.             continue;
  63.         
  64.         // Iterate a few hundred times to give the orbits a chance to get established
  65.         for (count = 1, x = 0.5; count < 100; count++)
  66.             x = a * x * (1.0 - x);
  67.         
  68.         // Now, draw for real
  69.         for (count = 1; count < 500; count++) {
  70.             x = a * x * (1.0 - x);
  71.             h = x * portWidth;
  72.             MoveTo(h, v);
  73.             Line(0, 0);
  74.         }
  75.  
  76.     }
  77. }
  78.